﻿namespace NTLib.Core.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// Define some extension method on the string object
    /// </summary>
    public static class StringExtensions
    {
        #region Methods

        /// <summary>
        /// Contract version of the String.format called.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static string Argument(this string str, params object[] args)
        {
            return string.Format(str, args);
        }

        /// <summary>
        /// Get the full string description of an exception. Inner exception included.
        /// </summary>
        /// <param name="ex">The ex.</param>
        public static string ToFullString(this Exception ex)
        {
            StringBuilder builder = new StringBuilder();
            var i = 0;
            while (ex != null)
            {
                var strs = ex.ToString();
                var strsLines = strs.Split('\n');

                foreach (var s in strsLines)
                {
                    builder.Append(' ', i);
                    builder.AppendLine(s);
                }
                ex = ex.InnerException;
                i += 4;
            }
            return builder.ToString();
        }

        #endregion 
    }
}
